﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Winowajca
{
    public partial class AddWine1 : System.Web.UI.UserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            using (DatabaseContext ctx = new DatabaseContext())
            {                
                foreach (var wineColor in ctx.WineColor)
                {
                    rblColor.Items.Add(new ListItem(wineColor.Name, wineColor.Id.ToString()));
                }

                foreach (var wineSugar in ctx.WineSugar)
                {
                    rblSugar.Items.Add(new ListItem(wineSugar.Name, wineSugar.ID.ToString()));
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Wine GetWine()
        {
            Wine wine = new Wine();
            wine.Name = txtName.Text;
            wine.Color = int.Parse(rblColor.SelectedValue);
            wine.Sugar = int.Parse(rblSugar.SelectedValue);
            wine.Origin = txtOrigin.Text;

            int rating;

            if (int.TryParse(rblRating.SelectedValue, out rating) && (new WineRatingValidator().ValidateRating(rating)))
            {
                wine.Rating = rating;
            }

            decimal price;
            if (decimal.TryParse(txtPrice.Text, out price))
            {
                wine.Price = price;
            }

            return wine;
        }
    }
}